package conf

import (
	"fmt"
	logging "github.com/sirupsen/logrus"
	"gopkg.in/ini.v1"
	"mall/model"
	"mall/pkg/utils"
	"strings"
)

var (

	AppMode  			string
	HttpPort 			string

	Db         			string
	DbHost     			string
	DbPort     			string
	DbUser     			string
	DbPassWord 			string
	DbName     			string

	AccessKey      		string
	SerectKey      		string
	Bucket     			string
	QiniuServer      	string

	ValidEmail 			string
	SmtpHost 			string
	SmtpEmail 			string
	SmtpPass 			string

	Encryption 			util.Encryption
)

func Init() {
	//从本地读取环境变量
	file, err := ini.Load("./conf/config.ini")
	if err != nil {
		fmt.Println("配置文件读取错误，请检查文件路径:", err)
	}
	LoadServer(file)
	LoadMysqlData(file)
	LoadQiniu(file)
	LoadEmail(file)
	if err := LoadLocales("conf/locales/zh-cn.yaml"); err != nil {
		logging.Info(err) //日志内容
		panic(err)
	}
	//MySQL
	pathRead := strings.Join([]string{DbUser, ":", DbPassWord, "@tcp(", DbHost, ":", DbPort, ")/", DbName, "?charset=utf8&parseTime=true"}, "")
	pathWrite := strings.Join([]string{DbUser, ":", DbPassWord, "@tcp(", DbHost, ":", DbPort, ")/", DbName, "?charset=utf8&parseTime=true"}, "")
	model.Database(pathRead, pathWrite)
}


func LoadServer(file *ini.File) {
	AppMode = file.Section("service").Key("AppMode").String()
	HttpPort = file.Section("service").Key("HttpPort").String()
}


func LoadMysqlData(file *ini.File) {
	Db = file.Section("mysql").Key("Db").String()
	DbHost = file.Section("mysql").Key("DbHost").String()
	DbPort = file.Section("mysql").Key("DbPort").String()
	DbUser = file.Section("mysql").Key("DbUser").String()
	DbPassWord = file.Section("mysql").Key("DbPassWord").String()
	DbName = file.Section("mysql").Key("DbName").String()
}


	func LoadQiniu(file *ini.File) {
		AccessKey = file.Section("qiniu").Key("AccessKey").String()
		SerectKey = file.Section("qiniu").Key("SerectKey").String()
		Bucket = file.Section("qiniu").Key("Bucket").String()
		QiniuServer = file.Section("qiniu").Key("QiniuServer").String()
	}

func LoadEmail(file *ini.File) {
	ValidEmail = file.Section("email").Key("ValidEmail").String()
	SmtpHost= file.Section("email").Key("SmtpHost").String()
	SmtpEmail= file.Section("email").Key("SmtpEmail").String()
	SmtpPass= file.Section("email").Key("SmtpPass").String()
}